package com.hb.B_面向对象.E_密封类_代数类型

/**
 * 场景：
 *  我们想根据一些条件来计算下面几种图形的面积：
 *      圆形（给定半径）
 *      长方形（给定长和宽）
 *      三角形（给定底和高）
 *  我们怎么把上面的图形抽象成ADT呢？首先，找到他们的共同点，即他们都是集合图形（Shape）,
 *  然后我们就可以利用密封类来进行抽象。
 */
fun main() {
    val circle = Shape.Circle(2.0)
    println(getArea(circle))
    val listOf = listOf(1, 2)
    listOf.map {

    }
}

/**
public abstract class Shape {
private Shape() {}

public static final class Circle extends Shape {}

public static final class Rectangle extends Shape {}
public static final class Triangle extends Shape {}
}
 */
sealed class Shape{
    //下面的类相当于静态内部类
    /**
     *
    public static final class Circle extends Shape {
    private final double redius;
    public final double getRedius() {
    return this.redius;
    }
    public Circle(double redius) {
    super(null);
    this.redius = redius;
    }
    }
     */
    class Circle(val redius:Double):Shape()
    class Rectangle(val width:Double,val height:Double):Shape()
    class Triangle(val base:Double,val height:Double):Shape()

}


fun getArea(shape: Shape):Double=when(shape){
    is Shape.Circle -> Math.PI*shape.redius*shape.redius
    is Shape.Rectangle -> shape.width*shape.height
    is Shape.Triangle -> shape.base*shape.height/2.0
}

